package cn.touna.jss.modules.third.gzt;

import cn.id5.gboss.GbossClient;
import cn.id5.gboss.GbossConfig;
import cn.id5.gboss.http.HttpResponseData;
import cn.touna.risk.api.contraints.facet.First;
import cn.touna.risk.api.enums.SystemErrorCodeEnum;
import cn.touna.jss.common.factory.ResponseFactory;
import cn.touna.jss.common.properties.disconf.GuozhengtongProperties;
import cn.touna.jss.common.util.StaxonUtil;
import cn.touna.jss.common.util.ValidUtil;
import cn.touna.jss.modules.adapter.ApiMapping;
import cn.touna.jss.modules.third.gzt.entity.GuoZhengTongEntity;
import cn.touna.risk.api.domain.Response;
import cn.touna.risk.api.utils.ServiceContext;
import com.alibaba.fastjson.JSONObject;
import org.apache.http.HttpStatus;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * 国政通
 * Created by wuwc on 2017/7/19.
 */
@Service
public class GuoZhengTongService {

    private final static Logger logger = LogManager.getLogger(GuoZhengTongService.class);

    @Autowired
    private GuozhengtongProperties guozhengtongProperties;

    private GbossConfig getConfig() {
        GbossConfig config = new GbossConfig();
        config.setEndpoint(guozhengtongProperties.getUrl());
        config.setDesKey(guozhengtongProperties.getKey());
        config.setEncrypted(true);
        //分配帐号
        config.setAccount(guozhengtongProperties.getUsername());
        //分配密码
        config.setAccountpwd(guozhengtongProperties.getUserpwd());
        config.setDesCharset("GB18030");
        config.setTimeout(15000);
        return config;
    }

    @ApiMapping("gzt.personIdentify")
    public Response personIdentify(GuoZhengTongEntity guoZhengTongEntity) {
        Response response = new Response<>();
        try {
            //校验
            Response validResponse = ValidUtil.valid(guoZhengTongEntity, First.class);
            if (validResponse != null)
                return validResponse;
            GbossClient client = new GbossClient(getConfig());
            StringBuffer param = new StringBuffer();
            param.append(guoZhengTongEntity.getName()).append(",");
            param.append(guoZhengTongEntity.getIdCard()).append(",");
            param.append(ServiceContext.genUniqueId()).append(",");
            param.append(guoZhengTongEntity.isFlee() == true ? true : false).append(",");
            param.append(guoZhengTongEntity.isBreak() == true ? true : false).append(",");
            param.append(guoZhengTongEntity.isDrug() == true ? true : false).append(",");
            param.append(guoZhengTongEntity.isTakeDrugs() == true ? true : false);
            HttpResponseData httpdata = client.invokeSingle(guozhengtongProperties.getType(), param.toString());
            logger.info("gboss 调用耗时:{},httpdata:{} ", httpdata.getTime(), httpdata.getData());
            if (httpdata.getStatus() == HttpStatus.SC_OK) {
                String resultObjStr = StaxonUtil.xml2json(httpdata.getData());
                JSONObject json = (JSONObject) JSONObject.parse(resultObjStr);
                JSONObject data = json.getJSONObject("data");
                response.setData(data);
                return response.success(SystemErrorCodeEnum.SYSTEM_REQUEST_SUCC);
            }else{
                return ResponseFactory.fail(SystemErrorCodeEnum.JSS_CALL_EXTSYSTEM_ERROR,"httpStatus:" + httpdata.getStatus());
            }
        } catch (Exception e) {
            logger.error("", e);
            return ResponseFactory.fail(SystemErrorCodeEnum.JSS_CALL_EXTSYSTEM_ERROR,e.getMessage());
        }
    }
}
